package com.example.graduation.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.graduation.entity.Notice;
import com.example.graduation.entity.User;
import com.example.graduation.exception.MyException;
import com.example.graduation.mapper.NoticeMapper;
import com.example.graduation.mapper.UserMapper;
import com.example.graduation.service.NoticeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.graduation.util.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xyc
 * @since 2022-03-23
 */
@Service
public class NoticeServiceImpl extends ServiceImpl<NoticeMapper, Notice> implements NoticeService {


    @Resource
    private NoticeMapper noticeMapper;

    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private UserMapper userMapper;

    /**
     * 发送消息通知
     *
     * @return {@link null}
     * @author xieyucan
     * CreateDate 2022/3/23 14:51
     */
    @Override
    public Notice sendNotice(Notice notice) {
        try {
            noticeMapper.insert(notice);
            return notice;
        } catch (Exception e) {
            throw new RuntimeException("操作异常");
        }
    }


    /**
     * 查询用户发送或者受到的消息通知
     *
     * @param token
     * @return {@link Page< Notice>}
     * @author xieyucan
     * CreateDate 2022/3/23 15:13
     */
    @Override
    public Page<Notice> findAllNotice(String token, Integer sendoraccept, Integer page, Integer size) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 8;
        }
        try {
            int userId = jwtUtil.getUserIdByToken(token);
            QueryWrapper<Notice> wrapper = new QueryWrapper<>();
            if (sendoraccept == 0) {
                wrapper.eq("acceptId", userId).orderByDesc("createTime");
                Page<Notice> noticePage = noticeMapper.selectPage(new Page<>(page, size), wrapper);
                return noticePage;
            } else if (sendoraccept == 1) {
                wrapper.eq("sendId", userId).orderByDesc("createTime");
                Page<Notice> noticePage = noticeMapper.selectPage(new Page<>(page, size), wrapper);
                return noticePage;
            } else {
                throw new MyException("查询失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e.getMessage());
        }
    }


    /**
     * 查询超级管理员收到的消息，当type为null时，搜索所有消息
     *
     * @param token
     * @param type
     * @param page
     * @param size
     * @return
     */
    public Page<Notice> findNoticeByType(String token, Integer type, Integer page, Integer size) {
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 8;
        }
        try {
            int userId = jwtUtil.getUserIdByToken(token);
            QueryWrapper<Notice> wrapper = new QueryWrapper<>();
            if (type != null) {
                wrapper.eq("type", type);
            }
            wrapper.eq("acceptId", userId);
            Page<Notice> page1 = noticeMapper.selectPage(new Page<>(page, size), wrapper);
            return page1;
        } catch (Exception e) {
            throw new RuntimeException("操作失败");
        }
    }


    /**
     * 查看发送消息
     *
     * @param
     * @param noticeId
     * @return
     */
    public Notice checkNotices(Integer noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        Integer acceptid = notice.getAcceptid();
        User user = userMapper.selectById(acceptid);
        notice.setUser(user);
        return notice;

    }


    /**
     * 查看接受消息
     *
     * @param
     * @param noticeId
     * @return
     */
    public Notice checkNoticea(Integer noticeId) {
        Notice notice = noticeMapper.selectById(noticeId);
        Integer acceptid = notice.getSendid();
        User user = userMapper.selectById(acceptid);
        notice.setUser(user);
        return notice;
    }


    /**
     * 查找用户发送的消息
     *
     * @param token
     * @return
     */
    public List<Notice> findSend(String token) {
        int userId = jwtUtil.getUserIdByToken(token);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("sendId", userId).orderByDesc("createTime");
        List<Notice> notices = noticeMapper.selectList(wrapper);
        for (Notice notice : notices) {
            User user = userMapper.selectById(notice.getAcceptid());
            notice.setUser(user);
        }
        return notices;
    }


    /**
     * 查找用户接受到的消息
     *
     * @param token
     * @return
     */
    public List<Notice> findaccept(String token) {
        int userId = jwtUtil.getUserIdByToken(token);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("acceptId", userId).orderByDesc("createTime");
        List<Notice> notices = noticeMapper.selectList(wrapper);
        for (Notice notice : notices) {

            User user = userMapper.selectById(notice.getSendid());
            notice.setUser(user);
        }
        return notices;
    }


    /**
     * 查看管理员接收的消息
     *
     * @param token
     * @return
     */
    public List<Notice> findaccept1(String token, Integer type) {
        int userId = jwtUtil.getUserIdByToken(token);
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("acceptId", userId).eq("type", type).orderByDesc("createTime");
        List<Notice> notices = noticeMapper.selectList(wrapper);
        for (Notice notice : notices) {

            User user = userMapper.selectById(notice.getSendid());
            notice.setUser(user);
        }
        return notices;
    }


    public Notice findByid(Integer id) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Notice notices = noticeMapper.selectOne(wrapper);
        User user = userMapper.selectById(notices.getSendid());
        notices.setUser(user);
        return notices;
    }


    public Notice ok(Integer id) {
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        Notice notices = noticeMapper.selectOne(wrapper);
        notices.setStatus(1);
        noticeMapper.update(notices, wrapper);
        return notices;
    }


}
